from flask import Flask,session,g
import config
from exts import db,mail
from bleuprints.qa import bp as qa_bp
from bleuprints.auth import bp as auth_bp
from models import UserModel
from flask_migrate import Migrate


app = Flask(__name__)
app.config.from_object(config)

db.init_app(app)
mail.init_app(app)

migrate = Migrate(app, db)

app.register_blueprint(qa_bp)
app.register_blueprint(auth_bp)

@app.before_request
def my_before_request():
    user_id = session.get("user_id")
    if user_id:
        user = UserModel.query.get(user_id)
        setattr(g,"user",user)
    else:
        setattr(g,"user",None)

#所有模板中可直接使用user变量
@app.context_processor
def my_context_processor():
    return {"user":g.user}


@app.route('/')
def hello_world():  # put application's code here
    return 'Hello World!'


if __name__ == '__main__':
    app.run(debug=True)
#flask db init
#flask db migrate
#flask db upgrade